package cn.com.zpark;

import cn.com.zpark.c3p0.C3P0Utils01;
import cn.com.zpark.c3p0.C3P0Utils02;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @PackageName: cn.com.zpark.jdbc
 * @ClassName: JDBC_01_Select
 * @Description:
 * @author: RZS
 * @date: 2021/10/26  16:48
 */
public class JDBC_03_Select {
    /**
     * 使用jdbc的步骤：
     *      1、注册驱动
     *      2、获取连接
     *      3、创建statement对象
     *      4、编写sql
     *      5、执行sql
     *      6、获取查询结果
     *      7、关闭连接
     * @param args
     */
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet res = null;
        try {

            // 2、获取连接
            conn = C3P0Utils02.getConn();
            // 4、编写sql
            String sql = "select * from user";
            // 3、创建statement对象
            stat = conn.prepareStatement(sql);

            // 5、执行sql
            res = stat.executeQuery();

            // 6、获取查询结果
            while (res.next()) {
                int id = res.getInt("id");
                String name = res.getString("name");
                String pwd = res.getString("pwd");
                double money = res.getDouble("money");
                System.out.print(id);
                System.out.print(name);
                System.out.print(pwd);
                System.out.print(money);
                System.out.println();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            // 7、关闭连接
            C3P0Utils02.close(res, conn, stat);
        }
    }
}
